package com.chukun.netty.grpc;

import io.grpc.Server;
import io.grpc.ServerBuilder;
import java.io.IOException;

/**
 * @author chukun
 * grpc服务端
 */
public class GrpcServer {

    private Server server;
    private void serverStart() throws IOException {
        this.server = ServerBuilder.forPort(8899).addService(new GrpcStudentService()).build().start();

        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            System.out.println("关闭jvm");
            GrpcServer.this.stop();
        }));
    }

    private void stop(){
        if(null !=server){
            this.server.shutdown();
        }
    }

    private void serverTermination() throws InterruptedException {
        if(null !=server){
            this.server.awaitTermination();
        }
    }

    public static void main(String[] args) throws InterruptedException, IOException {
        GrpcServer grpcServer = new GrpcServer();
        grpcServer.serverStart();
        //不调用此方法，grpc服务端启动就会停止
        grpcServer.serverTermination();
    }
}
